﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using Castle.Core;

namespace NSsh.Common.Utility
{
    public static class Dependency
    {
        private static IWindsorContainer _container = new WindsorContainer();

        public static IWindsorContainer Container
        {
            get { return _container; }
            set { _container = value; }
        }

        public static void Clear()
        {
            _container = new WindsorContainer();
        }

        public static TService Resolve<TService>()
        {
            return _container.Resolve<TService>();
        }

        public static TService Resolve<TService>(string key)
        {
            return _container.Resolve<TService>(key);
        }

        public static void RegisterTransient<TService, TImplementation>(string key) where TImplementation : class
        {
            _container.AddComponentWithLifestyle<TService, TImplementation>(key, LifestyleType.Transient);
        }

        public static void RegisterSingleton<TService, TImplementation>(string key) where TImplementation : class
        {
            _container.AddComponentWithLifestyle<TService, TImplementation>(key, LifestyleType.Singleton);
        }

        public static void RegisterInstance<TService>(string key, object instance)
        {
            _container.Kernel.AddComponentInstance(key, typeof(TService), instance);
        }
    }
}